#!/usr/bin/env node

const { execSync } = require("child_process");
const fs = require("fs");

console.log("开始测试1000句项目各个组件...\n");

// 测试函数
function testComponent(name, command, expectedFiles = []) {
  console.log(`正在测试: ${name}`);
  console.log(`执行命令: ${command}`);

  try {
    // 执行命令
    execSync(command, { stdio: "inherit" });

    // 检查预期文件是否存在
    let allFilesExist = true;
    for (const file of expectedFiles) {
      if (!fs.existsSync(file)) {
        console.log(`  ⚠️  警告: 预期文件不存在: ${file}`);
        allFilesExist = false;
      }
    }

    if (allFilesExist && expectedFiles.length > 0) {
      console.log(`  ✅ 成功: 所有预期文件都已生成`);
    }

    console.log(`  ✅ ${name} 测试通过\n`);
    return true;
  } catch (error) {
    console.log(`  ❌ ${name} 测试失败: ${error.message}\n`);
    return false;
  }
}

// 测试配置
const tests = [
  {
    name: "DAT到ASS转换",
    command: "node src/convert_dat_to_ass.js 1.dat",
    expectedFiles: ["srt_output/1.ass"],
  },
  {
    name: "批量转换所有DAT文件",
    command: "node src/convert_all.js",
    expectedFiles: [],
  },
  {
    name: "关键词提取测试",
    command: "node scripts/test_keywords.js srt_output/1.srt",
    expectedFiles: [],
  },
  {
    name: "图片下载功能",
    command: "node src/download_images_simple.js srt_output/1.srt 5",
    expectedFiles: [],
  },
];

// 运行测试
let passedTests = 0;
let totalTests = tests.length;

for (const test of tests) {
  const passed = testComponent(test.name, test.command, test.expectedFiles);
  if (passed) passedTests++;
}

// 输出测试结果
console.log("\n==================== 测试结果 ====================");
console.log(`通过测试: ${passedTests}/${totalTests}`);

if (passedTests === totalTests) {
  console.log("🎉 所有测试都通过了！");
} else {
  console.log("❌ 部分测试失败，请检查上面的错误信息。");
}

console.log("==================================================");
